<?php

namespace Modules\Web\Http\Controllers;

use HiCommon\Repository\BannerRepository;
use HiCommon\Service\CommonService;
use HiCommon\Service\LoginShopUserService;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class WebController extends BaseController
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        dd('web');
        return view('web::index');
    }

    /**
     * 用户登录-后台
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function me(){
        return  hello_success('成功',web_user()->toarray());
    }

    /**
     * 用户登录-后台
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function login_in(){
        try {
            $arr_input = Request::input();//登录表单信息
            $arr_input['ip'] = (string)Request::ip();//登录ip
            return LoginShopUserService::app_login_in($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 发送手机验证码接口
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function send_phone_code()
    {
        try {
            $s_phone = str_trim(Request::input('phone',''));//发送验证码手机号
            return CommonService::api_send_phonecode($s_phone);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 手机验证码登录
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function phonecode_login_in()
    {
        try {
            $s_phone = Request::input('phone','');//手机号
            $s_code = Request::input('code','');//验证码
            $s_ip = (string)Request::ip();//登录ip
            return LoginShopUserService::app_phonecode_login($s_phone,$s_code,$s_ip);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 修改密码
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_back_password(){
        try {
            $s_phone = Request::input('phone','');//手机号
            $s_code = Request::input('code','');//验证码
            $s_password = Request::input('password','');//密码
            return LoginShopUserService::app_getback_password($s_phone,$s_code,$s_password);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 单图片上传
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function single_img_upload(){
        $res = upload_single_file_by_binarystream($_FILES['file']);
        return $res;
    }

    /**
     * 多图片上传
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function multiple_imgs_upload(){
        try {
            $arr_base64_file = Request::input('file');
            $res = upload_multiple_files_by_base64($arr_base64_file);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 富文本上传
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function test_fwb(){
        try {
            $arr_input = Request::input();
            $res = DB::table('atest')->insert($arr_input);
            if(!$res){
                return hello_error('失败1');
            }
            else{
                return  hello_success('成功');
            }
            
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     * 获取富文本
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_test_fwb(){
        try {
            $n_id = intval(Request::input('id',0));
            $res = DB::table('atest')->find($n_id);
            if(is_null($res)){
                return hello_error('没有数据');
            }
            else{

                return  hello_success('成功',$res);
            }
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }

    }

    /**
     *  获取app启动页的banner图片
     *  hinq 2020 1 15
     *
     **/
    public function get_banner(){
        try {
//            $res = DB::table('banner')->where('type',5)->orderBy('sort','asc')->select('id','img','title')->get();

            $arr_option['where']=['type'=>5 , 'status'=>1];
            $arr_option['field']=['id','img','title'];
            $arr_option['order'] = ['key'=>'sort' , 'value'=>'asc'];
            $arr_list = BannerRepository::get_all( (array) $arr_option )->toarray();

            foreach ( $arr_list as $key=>&$vo){
                $vo['img'] = oss_full_url( $vo['img'] );
            }
            return  hello_success('ok',$arr_list);
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }




}
